package com.sgl.gk.controller;

import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.utils.ServletUtils;
import com.sgl.gk.controller.vo.UserLoginLogVo;
import com.sgl.gk.domain.GkSettingLog;
import com.sgl.gk.service.GkSettingLogService;
import com.sgl.system.domain.SysLogininfor;
import com.sgl.system.mapper.SysLogininforMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:xuenci
 * @date:2023/2/13 9:57
 **/
@RestController
@RequestMapping("/gk/settingLog")
public class GkSettingLogController extends BaseController {

    @Autowired
    GkSettingLogService gkSettingLogService;

    @Autowired
    SysLogininforMapper sysLogininforMapper;

    /**
     * 查询系统设置信息
     * @param gkSettingLog
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(GkSettingLog gkSettingLog){
        List<GkSettingLog> gkSettingLogList = gkSettingLogService.list(gkSettingLog);
        return getDataTable(gkSettingLogList);
    }

    /**
     * 添加系统设置信息
     * @param gkSettingLog
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(GkSettingLog gkSettingLog){
        int row = gkSettingLogService.add(gkSettingLog);
        return AjaxResult.success();
    }

    /**
     * 修改系统设置信息
     * @param gkSettingLog
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult edit(GkSettingLog gkSettingLog){
        int row = gkSettingLogService.edit(gkSettingLog);
        return AjaxResult.success();
    }

    /**
     * 删除系统设置信息
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public AjaxResult removeByIds(@PathVariable("ids")Long[] ids){
        int row = gkSettingLogService.removeByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 获取用户登陆日志
     * @param sysLogininfor
     * @return
     */
    @GetMapping("getLoginInfo")
    public TableDataInfo getLoginInfo(SysLogininfor sysLogininfor){
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        sysLogininfor.setStatus("0");
        sysLogininfor.setMsg("登录成功");
        List<SysLogininfor> sysLogininfors = sysLogininforMapper.selectLogininforList(sysLogininfor);
        long total = getDataTable(sysLogininfors).getTotal();
        List<UserLoginLogVo> userLoginLogVos = new ArrayList<>();
        for (SysLogininfor logininfor : sysLogininfors) {
            UserLoginLogVo userLoginLogVo = new UserLoginLogVo();
            userLoginLogVo.setInfoId(logininfor.getInfoId());
            userLoginLogVo.setUserName(logininfor.getUserName());
            userLoginLogVo.setLoginTime(logininfor.getLoginTime());
            userLoginLogVos.add(userLoginLogVo);
        }
        TableDataInfo dataTable = getDataTable(userLoginLogVos);
        dataTable.setTotal(total);
        return dataTable;
    }

}
